<template>
  <div class="login-container">
    <div class="login-box">
      <div class="left-box">
        <div class="big-logo">
          <img src="@/assets/img/common/logo.png" alt="" />
        </div>
        <div class="text">
          <span class="wel">欢迎使用</span>
          <span class="card">恒星学院校园一卡通服务平台</span>
        </div>
        <div class="img">
          <!-- <img src="@/assets/img/common/logo.png" alt="" /> -->
          <img src="@/assets/img/login/computer.png" alt="" />
        </div>
      </div>
      <div class="right-box">
        <div class="right-title">
          <p>welcome</p>
        </div>
        <div class="login-form">
          <!-- 登录表单 -->
          <el-form :model="loginForm" :rules="loginFormRules" ref="loginFormRef">
            <el-form-item prop="studentNum">
              <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="loginForm.studentNum"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input
                show-password
                type="password"
                placeholder="请输入密码"
                prefix-icon="el-icon-lock"
                v-model="loginForm.password"
              ></el-input>
            </el-form-item>
          </el-form>
          <div class="footer">
            <el-button class="login-btn" type="primary" @click="login">登录</el-button>
            <!-- <div class="forget-pwd" @click="forgetPwd">
              <p>忘记密码</p>
            </div> -->
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import commonServe from '../api/service/common.serve'
import { mapMutations } from 'vuex'
export default {
  data() {
    return {
      loginForm: {
        studentNum: '',
        password: ''
      },
      loginFormRules: {
        studentNum: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 7, max: 7, message: '账号长度为7个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度为6-15个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapMutations(['saveStudentId']),
    login() {
      // 登录之前的预校验
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return false // 未通过校验直接return
        commonServe
          .login(this.loginForm)
          .then((res) => {
            console.log(res)
            if (res.code === 200) {
              window.sessionStorage.setItem('studentId', res.data.id) //存本地
              this.saveStudentId(res.data.id) //存store
              this.$message.success('登录成功')
              this.$router.push('/home')
            } else {
              this.$message.warning(res.message || '用户名或密码错误')
            }
          })
          .catch((err) => {
            this.$message.warning(err.message || '登录失败')
          })
      })
    },
    forgetPwd() {
      console.log('忘记密码')
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
  background-image: url('@/assets/img/login/bgc.png');
  background-size: 100%;
  background-repeat: no-repeat;
  .login-box {
    display: flex;
    width: 1079px;
    height: 585px;
    background-color: #3a62d7;
    border-radius: 10px;
    .left-box {
      width: 639px;
      .big-logo {
        padding-top: 10px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .text {
        padding: 20px 0 0 50px;
        color: #fff;
        font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
        font-weight: bold;
        display: flex;
        flex-direction: column;
        .wel {
          font-size: 40px;
          padding-bottom: 0.625rem;
        }
        .card {
          font-size: 24px;
        }
      }
      .img {
        // height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
    .right-box {
      width: 440px;
      //   border: 1px solid black;
      border-radius: 10px;
      background-color: #fff;
      .right-title {
        p {
          text-align: center;
          font-size: 24px;
          font-weight: bold;
          color: #3a62d7;
        }
      }
      .login-form {
        // border: .0625rem solid black;
        padding: 100px 10px 0 10px;
        .footer {
          padding-top: 150px;
          .login-btn {
            width: 100%;
          }
          // .forget-pwd {
          //   p {
          //     width: 100%;
          //     color: #3a62d7;
          //     text-align: center;
          //     cursor: pointer;
          //   }
          // }
        }
      }
    }
  }
}
</style>
